﻿using com.fmtx.IDal;
using com.fmtx.Model;
using fmtx.Framework.DALFactory;
using fmtx.Framework.SqlHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace website.Areas.Admin.Controllers
{
    public class PlaceController : AdminBaseController
    {
        private IPlace _place = DataAccess<IPlace>.GetObject();

        //
        // GET: /Admin/Place/

        /// <summary>
        /// 城市列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="countryId"></param>
        /// <param name="country"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public ActionResult Index(string name, string country, int countryId = 0, int index = 0)
        {
            int count;
            var models = _place.Query(index, 12, name, countryId, out count);
            ViewBag.count = count;
            return View(models);
        }

        /// <summary>
        /// 城市编辑页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int? id)
        {
            Place model;
            if (id != null)
            {
                model = _place.Get(id.Value);
            }
            else
            {
                model = new Place();
            }
            return View(model);
        }

        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="bean"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Place bean)
        {
            if (ModelState.IsValid)
            {
                var filepic = Request.Files["filepic"];
                var filemark = Request.Files["filemark"];
                if (filepic != null && filepic.ContentLength > 0)
                {
                    bean.Pic = StaticCs.SaveRequestFile(filepic);
                }
                
                if (bean.Id > 0)
                {
                    var omodel = _place.Get(bean.Id);
                    omodel.Name = bean.Name;
                    omodel.Summary = bean.Summary;
                    omodel.Pic = bean.Pic;
                    omodel.Lat = bean.Lat;
                    omodel.Lng = bean.Lng;
                    omodel.PingYin = bean.PingYin;
                    omodel.EnName = bean.EnName;
                    _place.Update(omodel);
                }
                else
                {
                    _place.Add(bean);
                }
            }
            return View(bean);
        }



    }
}
